<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/29
 * Time: 14:51
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Committee extends Base
{
    /**
     * 组委会列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('committee')->count();// 获取总条数
            $lists = Db::name('committee')
                ->field('com_id, com_name, com_status, com_create_time')
                ->page($Nowpage,$limits)
                ->order('com_create_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加组委会
     * @return mixed|\think\response\Json
     */
    public function comAdd(){
        if(Request::instance()->isPost()){
            $com_name = trim(input('com_name'), ' ');
            $insert = [
                'com_name' => $com_name,
                'com_create_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_committee')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('committee/comadd');
    }
    
    /**
     * 编辑组委会
     * @return mixed|\think\response\Json
     */
    public function comEdit(){
        $com_id = input('com_id');
        if(Request::instance()->isPost()){
            $com_name = trim(input('com_name'), ' ');
            $update = [
                'com_name' => $com_name,
            ];
            $res = Db::table('up_committee')->where('com_id', $com_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $com = Db::table('up_committee')
            ->field('com_name')
            ->where('com_id', $com_id)
            ->find();
        $this->assign([
            'com_id' => $com_id,
            'com' => $com
        ]);
        return $this->fetch('committee/comedit');
    }
    
    /**
     * 禁用
     * @return \think\response\Json
     */
    public function comDown(){
        $com_id = input('id');
        $res = Db::table('up_committee')->where('com_id', $com_id)->setField('com_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function comUp(){
        $com_id = input('id');
        $res = Db::table('up_committee')->where('com_id', $com_id)->setField('com_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function comDel(){
        $com_id = input('id');
        $res = Db::table('up_committee')->delete($com_id);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 组委会管理员列表
     * @return mixed|\think\response\Json
     */
    public function comMember(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('committee_member')
                ->where('cm_committee_id', session('com_id'))
                ->where('cm_member_type', 1)
                ->count();// 获取总条数
            $lists = Db::name('committee_member')
                ->field('cm_id, cm_admin_name, cm_admin_phone, cm_login_ID, cm_time, cm_lose_time')
                ->where('cm_committee_id', session('com_id'))
                ->where('cm_member_type', 1)
                ->page($Nowpage,$limits)
                ->order('cm_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $com_id = input('com_id');
        if(isset($com_id) && !empty($com_id)) session('com_id', $com_id);
        $num = Db::table('up_committee_member')
            ->where('cm_committee_id', session('com_id'))
            ->where('cm_member_type', 1)
            ->count();
        $this->assign('num', $num);
        return $this->fetch('committee/commember');
    }
    
    /**
     * 添加管理员
     * @return mixed|\think\response\Json
     */
    public function comMemberAdd(){
        if(Request::instance()->isPost()){
            $cm_admin_name = trim(input('cm_admin_name'), ' ');
            $cm_admin_phone = trim(input('cm_admin_phone'), ' ');
            $cm_login_ID = trim(input('cm_login_ID'), ' ');
            $cm_login_pwd = trim(input('cm_login_pwd'), ' ');
            $cm_member_role = input('cm_member_role');
            $cm_lose_time = input('cm_lose_time');
            $pwd = md5($cm_login_pwd);
            $exit = Db::table('up_committee_member')
                ->where('cm_login_ID', $cm_login_ID)
                ->where('cm_login_pwd', $pwd)
                ->find();
            if($exit){
                return json(['code' => 100, 'msg' => '该管理员账号已存在，请重新分配']);
            }
            if(empty($cm_lose_time)){
                return json(['code' => 100, 'msg' => '请选择账号失效时间']);
            }
            $insert = [
                'cm_committee_id' => session('com_id'),
                'cm_admin_name' => $cm_admin_name,
                'cm_admin_phone' => $cm_admin_phone,
                'cm_login_ID' => $cm_login_ID,
                'cm_login_pwd' => $pwd,
                'cm_member_role' => $cm_member_role,
                'cm_time' => date('Y-m-d H:i:s'),
                'cm_lose_time' => $cm_lose_time
            ];
            $res = Db::table('up_committee_member')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('committee/commemberadd');
    }
    
    /**
     * 编辑管理员
     * @return mixed|\think\response\Json
     */
    public function comMemberEdit(){
        $cm_id = input('cm_id');
        if(Request::instance()->isPost()){
            $cm_admin_name = trim(input('cm_admin_name'), ' ');
            $cm_admin_phone = trim(input('cm_admin_phone'), ' ');
            $cm_login_ID = trim(input('cm_login_ID'), ' ');
            $cm_login_pwd = trim(input('cm_login_pwd'), ' ');
            $cm_member_role = input('cm_member_role');
            $cm_lose_time = input('cm_lose_time');
            if(empty($cm_lose_time)){
                return json(['code' => 100, 'msg' => '请选择账号失效时间']);
            }
            $update = [
                'cm_admin_name' => $cm_admin_name,
                'cm_admin_phone' => $cm_admin_phone,
                'cm_login_ID' => $cm_login_ID,
                'cm_member_role' => $cm_member_role,
                'cm_lose_time' => $cm_lose_time
            ];
            if(!empty($cm_login_pwd)){
                $pwd = md5($cm_login_pwd);
                $exit = Db::table('up_committee_member')
                    ->where('cm_login_ID', $cm_login_ID)
                    ->where('cm_login_pwd', $pwd)
                    ->find();
                if($exit){
                    return json(['code' => 100, 'msg' => '该管理员账号已存在，请重新分配']);
                }
                $update['cm_login_pwd'] = md5($cm_login_pwd);
            }
            $res = Db::table('up_committee_member')->where('cm_id', $cm_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $member = Db::table('up_committee_member')
            ->field('cm_admin_name, cm_admin_phone, cm_login_ID, cm_member_role, cm_lose_time')
            ->where('cm_id', $cm_id)
            ->find();
        $this->assign([
            'cm_id' => $cm_id,
            'member' => $member
        ]);
        return $this->fetch('committee/commemberedit');
    }
    
    /**
     * 删除管理员
     * @return \think\response\Json
     */
    public function comMemberDel(){
        $cm_id = input('id');
        $res = Db::table('up_committee_member')->delete($cm_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}